package main

import (
	"bufio"
	"fmt"
	"io"
	"sync"

	// "io/ioutil"
	"os"
	"strings"

	_ "github.com/go-sql-driver/mysql"
)

var kindMap map[string]map[string]string
var kind string
var kindBtMap map[string]string = make(map[string]string, 0)

func processLine(line []byte) {
	if kindMap == nil {
		kindMap = make(map[string]map[string]string)
	}

	//os.Stdout.Write(line)
	lineStr := string(line)
	if strings.Contains(lineStr, "|") {
		vs := strings.Split(lineStr, "|")
		m := kindMap[kind]
		if m == nil {
			m1 := make(map[string]string)
			kindMap[kind] = m1
		}
		kindMap[kind][vs[0]] = vs[1]
	} else {
		kb := strings.Split(lineStr, ":")
		kind = removeN(kb[0])
		kindBtMap[kind] = removeN(kb[1])
	}

}

func ReadLine(filePth string, hookfn func([]byte)) error {
	f, err := os.Open(filePth)
	if err != nil {
		return err
	}
	defer f.Close()

	bfRd := bufio.NewReader(f)
	for {
		line, err := bfRd.ReadBytes('\n')
		hookfn(line)    //放在错误处理前面，即使发生错误，也会处理已经读取到的数据。
		if err != nil { //遇到任何错误立即返回，并忽略 EOF 错误信息
			if err == io.EOF {
				return nil
			}
			return err
		}
	}

}

type TsBzdm struct {
	kind string
	code string
	mc   string
}

/**
 * 判断是否存在，不存在的插入
 */
func existAndInsert() {
	ReadLine("D:/project/wsla/设计/1149/dict.txt", processLine)
	//fmt.Println(kindMap)

	for k, v := range kindMap {
		for k1, v1 := range v {
			// fmt.Printf("%s,%s,%s\n", k, k1, v1)
			//test(v1)
			kind := removeN(k)
			code := "09_" + kind + "-" + removeN(k1)
			mc := removeN(v1)
			bzdm := TsBzdm{kind: kind, code: code, mc: mc}
			notExistRow(bzdm)
		}
	}

	fmt.Println("---------------------------------------------")
	// for _,a := range notExist{
	// 	fmt.Println(a)
	// }
	for i, bzdm := range notExistR {
		var s = "INSERT INTO TS_BZDM (KIND,BT,CODE,MC,SFJY ,PXH ,CODENOW ,VER ) VALUES ('%s','%s','%s','%s','0',%d,'V09','09');"
		maxPxh := getMaxPxh(&bzdm.kind)
		fmt.Printf(s+"\n", bzdm.kind, kindBtMap[bzdm.kind], bzdm.code, bzdm.mc, maxPxh+i+1)
	}
}

var bzdmSql = "INSERT INTO TS_BZDM (KIND,BT,CODE,MC,SFJY ,PXH ,CODENOW ,VER ) VALUES ('%s','%s','%s','%s','0',%d,'V09','09');"

func bz_GF2021_XF_1023() {
	kind := "09003"
	max := getMaxPxh(&kind)
	i := 1
	FileReadLineDo("D:\\project\\wsla\\设计\\1149\\bz_GF2021-XF-1023.txt", func(s string) {
		split := strings.Split(s, "|")
		fmt.Printf(bzdmSql+"\n", kind, "信访来源", "zg"+"09_"+kind+"-"+split[0], split[1], i+max)
		i++
	})
}

var tudmSql = "INSERT INTO TU_DM(FYDM,KIND,CODE,SM,SFJY) VALUES('999999','%s','%s','%s','0');"

func GF2021_XF_1018() {
	kind := "YDSRGX"
	FileReadLineDo("D:/project/wsla/设计/1149/GF2021-XF-1018.txt", func(s string) {
		split := strings.Split(s, "|")
		fmt.Printf(tudmSql+"\n", kind, split[0], split[1])
	})
}

func InsertTsdm() {
	// bytes,_:=ioutil.ReadFile("D:\\project\\wsla\\设计\\1149\\GF2021-XF-1023.txt")
	// str:=string(bytes)
	// split:=strings.Split(str,"\n")
	// for _,line := range split{
	// 	fmt.Println(line)
	// }

	// arr := make([][]string,0)
	i := 1
	FileReadLineDo("D:\\project\\wsla\\设计\\1149\\GF2021-XF-1023.txt", func(s string) {
		//fmt.Println(sql)
		//split := strings.Split(strings.TrimRight(s,"\n"), "|")
		//arr = append(arr, split)
		// fmt.Println(s)
		v := strings.Split(s, "|")
		fmt.Printf("INSERT INTO TS_DM (KING,BT,CODE,MC,SFJY,PXH) VALUES ('%s','%s','%s','%s','%s',%d);\n", "GF2021-XF-1023", "信访来源", v[0], string(v[1]), "0", i)
		i++
	})
	// fmt.Println(arr)
	// for i,v := range arr{
	// 	//sql := "INSERT INTO TS_DM(KING,BT,CODE,MC,SFJY,PXH) VALUES('%s','%s','%s','%s','%s',%d);\n"
	// 	//fmt.Print(sql)

	// 	fmt.Printf("INSERT INTO TS_DM (KING,BT,CODE,MC,SFJY,PXH) VALUES ('%s','%s','%s','%s','%s',%d);\n", "GF2021-XF-1023", "信访来源", v[0], string(v[1]), "0", i+1)
	// }
}

func main() {
	// createDb(1)

	// existAndInsert()
	//InsertTsdm()
	// bz_GF2021_XF_1023()
	// GF2021_XF_1018()

	// readDirFiles("D:")
	play()
}

func play() {
	fmt.Println("new")
	wg := new(sync.WaitGroup)
	for i:=0;i<10000000;i++{
		wg.Add(1)
		go func() {
			httpGet("http://192.168.1.134:8092/lawsuit-inner")
			wg.Done()
		}()
	}
	wg.Wait()
	play()
}

func getMaxPxh(kind *string) int {
	rs, _ := createDb(dev).Query("SELECT MAX(PXH) FROM TS_BZDM WHERE KIND = ?", *kind)
	if rs.Next() {
		var maxPxh int
		rs.Scan(&maxPxh)
		return maxPxh
	}
	return 0
}

func removeN(v string) string {
	if strings.Contains(v, "\n") {
		strC := []rune(v)
		m := strC[0 : len(strC)-2]
		return string(m)
	}
	return v
}

var notExistR []TsBzdm

func notExistRow(bzdm TsBzdm) {
	rows, _ := createDb(dev).Query("SELECT KIND,CODE,MC FROM TS_BZDM WHERE KIND=? AND CODE=? and SFJY =0", bzdm.kind, bzdm.code)
	if rows.Next() {
		var existBzdm TsBzdm
		rows.Scan(&existBzdm.kind, &existBzdm.code, &existBzdm.mc)
		fmt.Println(bzdm, "存在,为", existBzdm)
	} else {
		notExistR = append(notExistR, bzdm)
	}
}

var notExist []string

func test(mc string) {
	strC := []rune(mc)
	m := strC[0 : len(strC)-2]
	fmt.Println("查询mc:", mc)
	rows, _ := createDb(dev).Query("SELECT KIND,CODE,MC FROM TS_BZDM WHERE MC = ?", string(m))
	defer rows.Close()
	if rows.Next() {
		var tsBzdm TsBzdm
		rows.Scan(&tsBzdm.kind, &tsBzdm.code, &tsBzdm.mc)
		fmt.Println(tsBzdm)
		fmt.Println("---------------------")
	} else {
		notExist = append(notExist, mc)
	}
}
